import { modules } from "../../gameEngine";

const { ccclass, property } = cc._decorator;

interface VideoCountType {
    currLookCount: number;
    setLookCount: number;
}

@ccclass
export default class o1DialogBox extends cc.Component {
    @property(cc.Node)
    showNode: cc.Node = null;
    @property(cc.Node)
    ndBg: cc.Node = null;
    @property(cc.Label)
    lbTitle: cc.Label = null;
    @property(cc.Label)
    lbMessage: cc.Label = null;
    @property(cc.Label)
    lbOk: cc.Label = null;
    @property(cc.Label)
    lbCancel: cc.Label = null;
    @property(cc.Label)
    lbVideoCount: cc.Label = null;
    @property(cc.Node)
    lookVideoNode: cc.Node = null;
    @property(cc.Node)
    btCancel: cc.Node = null;

    onLoad() {
        const O1HD = window['O1HD'];
        O1HD.getModules().uiControlModule.setGaussianMode(this.ndBg);
    }

    start() {

    }

    private m_videoCountInfo: VideoCountType = null;
    private m_callBack: (isOk: boolean) => void = null;
    private m_lookVideoSource: string = null;

    showAction() {
        modules().uiControlModule.showHideDialogAnimation(this.showNode, true);
        //

    }

    hideAction() {
        modules().uiControlModule.showHideDialogAnimation(this.showNode, false, () => {
            this.node.destroy();
        })
        //
    }

    showDialogBox(title: string, message: string, isCancelOk: boolean, clickCallBack: (isOk: boolean) => void,
        okButtonText: string = '确定', cancelButtonText: string = '取消', lookVideoCount: number = 0, lookVideoSource?: string) {
        this.m_lookVideoSource = lookVideoSource || '未传递目的';
        this.lbTitle.string = title;
        this.lbMessage.string = message;
        // this.btCancel.active = isCancelOk;
        this.m_callBack = clickCallBack;
        //要看视频次数
        if (lookVideoCount > 0) {
            this.m_videoCountInfo = {
                currLookCount: 0,
                setLookCount: lookVideoCount,
            }
        } else {
            this.m_videoCountInfo = null;
        }
        // this.lbOk.string = okButtonText;
        // this.lbCancel.string = cancelButtonText;
        this.onUpdateLookVideoCount();
        this.showAction();
    }

    onBtnOkCancel(event, custom: string) {
        if (this.m_videoCountInfo) {
            if (custom == 'ok') {
                //
                const O1HD = window['O1HD'];
                //
                if (O1HD == null) {
                    console.error('#95错误 框架引擎未初始化 || 未添加到全局变量', O1HD);
                    return;
                }
                cc.game.pause();
                O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
                    cc.game.resume();
                    this.m_videoCountInfo.currLookCount++;
                }, (e) => {
                    cc.game.resume();
                    O1HD.getModules().uiControlModule.showMessageBox('视频观看失败');
                    console.info("快手视频加载失败");
                }, () => {
                    cc.game.resume();
                    this.onUpdateLookVideoCount();
                    //
                    if (this.m_videoCountInfo.currLookCount >= this.m_videoCountInfo.setLookCount) {
                        this.m_callBack && this.m_callBack(true);
                        this.hideAction();
                    }
                }, this.m_lookVideoSource);
            } else {
                this.m_callBack && this.m_callBack(false);
                this.hideAction();
            }
        } else {
            this.m_callBack && this.m_callBack(custom == 'ok');
            this.hideAction();
        }
    }

    private m_thisClickTween: cc.Tween = null;
    private m_runIng: boolean = false;

    onBtnThis() {
        const width = 8;
        //
        if (this.m_thisClickTween == null) {
            let once = cc.tween(this.showNode)
                .call(() => {
                    this.m_runIng = true;
                })
                .by(0.02, { x: width, y: width })
                .by(0.02, { x: 0, y: -width })
                .by(0.02, { x: -width, y: -width })
                .by(0.02, { x: -width, y: 0 })
                .by(0.02, { x: 0, y: width })
                .by(0.02, { x: width, y: 0 });
            //
            this.m_thisClickTween = cc.tween(this.showNode)
                .then(once)
                .repeat(3)
                .call(() => {
                    this.m_runIng = false;
                });
        }
        //
        if (this.m_runIng) {
            return;
        }
        this.m_thisClickTween.start();
    }

    onUpdateLookVideoCount() {
        if (this.m_videoCountInfo) {
            // this.lookVideoNode.active = true;
            const { setLookCount, currLookCount } = this.m_videoCountInfo;
            this.lbVideoCount.string = setLookCount > 1 ? `(${currLookCount}/${setLookCount})` : '';
        } else {
            // this.lookVideoNode.active = false;
            this.lbVideoCount.string = '';
        }
    }

    // update (dt) {}
}













